package 复写零;

class Solution {
    //暴力法
    /*public void duplicateZeros(int[] arr) {
        int cur = arr.length - 1;
        int dest = 0;
        for (;dest < arr.length; dest++){
            if(arr[dest] == 0){
                while (cur-1 != dest && cur != dest){
                    arr[cur] = arr[cur-1];
                    cur--;
                }
                arr[cur] = 0;
                dest++;
                cur = arr.length - 1;
            }
        }
    }*/
    //双指针法
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        while (dest < arr.length-1){
            if (arr[cur] != 0){
                dest++;
            }else {
                dest+=2;
            }
            if (dest < arr.length-1){
                cur++;
            }
        }
        if (dest > arr.length-1){
            arr[dest-1] = 0;
            dest -= 2;
            cur--;
        }
        while (cur >= 0){
            if (arr[cur] != 0){
                arr[dest] = arr[cur];
                dest--;
            }else {
                arr[dest] = 0;
                arr[dest-1] = 0;
                dest-=2;
            }
            cur--;
        }
    }
}